home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / js / jsapi.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  77KB  |  1,984 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. #ifndef jsapi_h___
  41. #define jsapi_h___
  42. /*
  43.  * JavaScript API.
  44.  */
  45. #include <stddef.h>
  46. #include <stdio.h>
  47. #include "jspubtd.h"
  48.  
  49. JS_BEGIN_EXTERN_C
  50.  
  51. /*
  52.  * Type tags stored in the low bits of a jsval.
  53.  */
  54. #define JSVAL_OBJECT            0x0     /* untagged reference to object */
  55. #define JSVAL_INT               0x1     /* tagged 31-bit integer value */
  56. #define JSVAL_DOUBLE            0x2     /* tagged reference to double */
  57. #define JSVAL_STRING            0x4     /* tagged reference to string */
  58. #define JSVAL_BOOLEAN           0x6     /* tagged boolean value */
  59.  
  60. /* Type tag bitfield length and derived macros. */
  61. #define JSVAL_TAGBITS           3
  62. #define JSVAL_TAGMASK           JS_BITMASK(JSVAL_TAGBITS)
  63. #define JSVAL_TAG(v)            ((v) & JSVAL_TAGMASK)
  64. #define JSVAL_SETTAG(v,t)       ((v) | (t))
  65. #define JSVAL_CLRTAG(v)         ((v) & ~(jsval)JSVAL_TAGMASK)
  66. #define JSVAL_ALIGN             JS_BIT(JSVAL_TAGBITS)
  67.  
  68. /* Predicates for type testing. */
  69. #define JSVAL_IS_OBJECT(v)      (JSVAL_TAG(v) == JSVAL_OBJECT)
  70. #define JSVAL_IS_NUMBER(v)      (JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v))
  71. #define JSVAL_IS_INT(v)         (((v) & JSVAL_INT) && (v) != JSVAL_VOID)
  72. #define JSVAL_IS_DOUBLE(v)      (JSVAL_TAG(v) == JSVAL_DOUBLE)
  73. #define JSVAL_IS_STRING(v)      (JSVAL_TAG(v) == JSVAL_STRING)
  74. #define JSVAL_IS_BOOLEAN(v)     (JSVAL_TAG(v) == JSVAL_BOOLEAN)
  75. #define JSVAL_IS_NULL(v)        ((v) == JSVAL_NULL)
  76. #define JSVAL_IS_VOID(v)        ((v) == JSVAL_VOID)
  77. #define JSVAL_IS_PRIMITIVE(v)   (!JSVAL_IS_OBJECT(v) || JSVAL_IS_NULL(v))
  78.  
  79. /* Objects, strings, and doubles are GC'ed. */
  80. #define JSVAL_IS_GCTHING(v)     (!((v) & JSVAL_INT) && !JSVAL_IS_BOOLEAN(v))
  81. #define JSVAL_TO_GCTHING(v)     ((void *)JSVAL_CLRTAG(v))
  82. #define JSVAL_TO_OBJECT(v)      ((JSObject *)JSVAL_TO_GCTHING(v))
  83. #define JSVAL_TO_DOUBLE(v)      ((jsdouble *)JSVAL_TO_GCTHING(v))
  84. #define JSVAL_TO_STRING(v)      ((JSString *)JSVAL_TO_GCTHING(v))
  85. #define OBJECT_TO_JSVAL(obj)    ((jsval)(obj))
  86. #define DOUBLE_TO_JSVAL(dp)     JSVAL_SETTAG((jsval)(dp), JSVAL_DOUBLE)
  87. #define STRING_TO_JSVAL(str)    JSVAL_SETTAG((jsval)(str), JSVAL_STRING)
  88.  
  89. /* Lock and unlock the GC thing held by a jsval. */
  90. #define JSVAL_LOCK(cx,v)        (JSVAL_IS_GCTHING(v)                          \
  91.                                  ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v))    \
  92.                                  : JS_TRUE)
  93. #define JSVAL_UNLOCK(cx,v)      (JSVAL_IS_GCTHING(v)                          \
  94.                                  ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v))  \
  95.                                  : JS_TRUE)
  96.  
  97. /* Domain limits for the jsval int type. */
  98. #define JSVAL_INT_BITS          31
  99. #define JSVAL_INT_POW2(n)       ((jsval)1 << (n))
  100. #define JSVAL_INT_MIN           ((jsval)1 - JSVAL_INT_POW2(30))
  101. #define JSVAL_INT_MAX           (JSVAL_INT_POW2(30) - 1)
  102. #define INT_FITS_IN_JSVAL(i)    ((jsuint)((i)+JSVAL_INT_MAX) <= 2*JSVAL_INT_MAX)
  103. #define JSVAL_TO_INT(v)         ((jsint)(v) >> 1)
  104. #define INT_TO_JSVAL(i)         (((jsval)(i) << 1) | JSVAL_INT)
  105.  
  106. /* Convert between boolean and jsval. */
  107. #define JSVAL_TO_BOOLEAN(v)     ((JSBool)((v) >> JSVAL_TAGBITS))
  108. #define BOOLEAN_TO_JSVAL(b)     JSVAL_SETTAG((jsval)(b) << JSVAL_TAGBITS,     \
  109.                                              JSVAL_BOOLEAN)
  110.  
  111. /* A private data pointer (2-byte-aligned) can be stored as an int jsval. */
  112. #define JSVAL_TO_PRIVATE(v)     ((void *)((v) & ~JSVAL_INT))
  113. #define PRIVATE_TO_JSVAL(p)     ((jsval)(p) | JSVAL_INT)
  114.  
  115. /* Property attributes, set in JSPropertySpec and passed to API functions. */
  116. #define JSPROP_ENUMERATE        0x01    /* property is visible to for/in loop */
  117. #define JSPROP_READONLY         0x02    /* not settable: assignment is no-op */
  118. #define JSPROP_PERMANENT        0x04    /* property cannot be deleted */
  119. #define JSPROP_EXPORTED         0x08    /* property is exported from object */
  120. #define JSPROP_GETTER           0x10    /* property holds getter function */
  121. #define JSPROP_SETTER           0x20    /* property holds setter function */
  122. #define JSPROP_SHARED           0x40    /* don't allocate a value slot for this
  123.                                            property; don't copy the property on
  124.                                            set of the same-named property in an
  125.                                            object that delegates to a prototype
  126.                                            containing this property */
  127. #define JSPROP_INDEX            0x80    /* name is actually (jsint) index */
  128.  
  129. /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
  130. #define JSFUN_LAMBDA            0x08    /* expressed, not declared, function */
  131. #define JSFUN_GETTER            JSPROP_GETTER
  132. #define JSFUN_SETTER            JSPROP_SETTER
  133. #define JSFUN_BOUND_METHOD      0x40    /* bind this to fun->object's parent */
  134. #define JSFUN_HEAVYWEIGHT       0x80    /* activation requires a Call object */
  135. #define JSFUN_FLAGS_MASK        0xf8    /* overlay JSFUN_* attributes */
  136.  
  137. /*
  138.  * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
  139.  * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
  140.  * methods of a class prototype that are exposed as static methods taking an
  141.  * extra leading argument: the generic |this| parameter.
  142.  *
  143.  * If you set this flag in a JSFunctionSpec struct's flags initializer, then
  144.  * that struct must live at least as long as the native static method object
  145.  * created due to this flag by JS_DefineFunctions or JS_InitClass.  Typically
  146.  * JSFunctionSpec structs are allocated in static arrays.
  147.  */
  148. #define JSFUN_GENERIC_NATIVE    JSFUN_LAMBDA
  149.  
  150. /*
  151.  * Well-known JS values.  The extern'd variables are initialized when the
  152.  * first JSContext is created by JS_NewContext (see below).
  153.  */
  154. #define JSVAL_VOID              INT_TO_JSVAL(0 - JSVAL_INT_POW2(30))
  155. #define JSVAL_NULL              OBJECT_TO_JSVAL(0)
  156. #define JSVAL_ZERO              INT_TO_JSVAL(0)
  157. #define JSVAL_ONE               INT_TO_JSVAL(1)
  158. #define JSVAL_FALSE             BOOLEAN_TO_JSVAL(JS_FALSE)
  159. #define JSVAL_TRUE              BOOLEAN_TO_JSVAL(JS_TRUE)
  160.  
  161. /*
  162.  * Microseconds since the epoch, midnight, January 1, 1970 UTC.  See the
  163.  * comment in jstypes.h regarding safe int64 usage.
  164.  */
  165. extern JS_PUBLIC_API(int64)
  166. JS_Now();
  167.  
  168. /* Don't want to export data, so provide accessors for non-inline jsvals. */
  169. extern JS_PUBLIC_API(jsval)
  170. JS_GetNaNValue(JSContext *cx);
  171.  
  172. extern JS_PUBLIC_API(jsval)
  173. JS_GetNegativeInfinityValue(JSContext *cx);
  174.  
  175. extern JS_PUBLIC_API(jsval)
  176. JS_GetPositiveInfinityValue(JSContext *cx);
  177.  
  178. extern JS_PUBLIC_API(jsval)
  179. JS_GetEmptyStringValue(JSContext *cx);
  180.  
  181. /*
  182.  * Format is a string of the following characters (spaces are insignificant),
  183.  * specifying the tabulated type conversions:
  184.  *
  185.  *   b      JSBool          Boolean
  186.  *   c      uint16/jschar   ECMA uint16, Unicode char
  187.  *   i      int32           ECMA int32
  188.  *   u      uint32          ECMA uint32
  189.  *   j      int32           Rounded int32 (coordinate)
  190.  *   d      jsdouble        IEEE double
  191.  *   I      jsdouble        Integral IEEE double
  192.  *   s      char *          C string
  193.  *   S      JSString *      Unicode string, accessed by a JSString pointer
  194.  *   W      jschar *        Unicode character vector, 0-terminated (W for wide)
  195.  *   o      JSObject *      Object reference
  196.  *   f      JSFunction *    Function private
  197.  *   v      jsval           Argument value (no conversion)
  198.  *   *      N/A             Skip this argument (no vararg)
  199.  *   /      N/A             End of required arguments
  200.  *
  201.  * The variable argument list after format must consist of &b, &c, &s, e.g.,
  202.  * where those variables have the types given above.  For the pointer types
  203.  * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
  204.  * to the JS runtime, not to the calling native code.  The runtime promises
  205.  * to keep this memory valid so long as argv refers to allocated stack space
  206.  * (so long as the native function is active).
  207.  *
  208.  * Fewer arguments than format specifies may be passed only if there is a /
  209.  * in format after the last required argument specifier and argc is at least
  210.  * the number of required arguments.  More arguments than format specifies
  211.  * may be passed without error; it is up to the caller to deal with trailing
  212.  * unconverted arguments.
  213.  */
  214. extern JS_PUBLIC_API(JSBool)
  215. JS_ConvertArguments(JSContext *cx, uintN argc, jsval *argv, const char *format,
  216.                     ...);
  217.  
  218. #ifdef va_start
  219. extern JS_PUBLIC_API(JSBool)
  220. JS_ConvertArgumentsVA(JSContext *cx, uintN argc, jsval *argv,
  221.                       const char *format, va_list ap);
  222. #endif
  223.  
  224. /*
  225.  * Inverse of JS_ConvertArguments: scan format and convert trailing arguments
  226.  * into jsvals, GC-rooted if necessary by the JS stack.  Return null on error,
  227.  * and a pointer to the new argument vector on success.  Also return a stack
  228.  * mark on success via *markp, in which case the caller must eventually clean
  229.  * up by calling JS_PopArguments.
  230.  *
  231.  * Note that the number of actual arguments supplied is specified exclusively
  232.  * by format, so there is no argc parameter.
  233.  */
  234. extern JS_PUBLIC_API(jsval *)
  235. JS_PushArguments(JSContext *cx, void **markp, const char *format, ...);
  236.  
  237. #ifdef va_start
  238. extern JS_PUBLIC_API(jsval *)
  239. JS_PushArgumentsVA(JSContext *cx, void **markp, const char *format, va_list ap);
  240. #endif
  241.  
  242. extern JS_PUBLIC_API(void)
  243. JS_PopArguments(JSContext *cx, void *mark);
  244.  
  245. #ifdef JS_ARGUMENT_FORMATTER_DEFINED
  246.  
  247. /*
  248.  * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
  249.  * The handler function has this signature (see jspubtd.h):
  250.  *
  251.  *   JSBool MyArgumentFormatter(JSContext *cx, const char *format,
  252.  *                              JSBool fromJS, jsval **vpp, va_list *app);
  253.  *
  254.  * It should return true on success, and return false after reporting an error
  255.  * or detecting an already-reported error.
  256.  *
  257.  * For a given format string, for example "AA", the formatter is called from
  258.  * JS_ConvertArgumentsVA like so:
  259.  *
  260.  *   formatter(cx, "AA...", JS_TRUE, &sp, &ap);
  261.  *
  262.  * sp points into the arguments array on the JS stack, while ap points into
  263.  * the stdarg.h va_list on the C stack.  The JS_TRUE passed for fromJS tells
  264.  * the formatter to convert zero or more jsvals at sp to zero or more C values
  265.  * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
  266.  * (via *app) to point past the converted arguments and their result pointers
  267.  * on the C stack.
  268.  *
  269.  * When called from JS_PushArgumentsVA, the formatter is invoked thus:
  270.  *
  271.  *   formatter(cx, "AA...", JS_FALSE, &sp, &ap);
  272.  *
  273.  * where JS_FALSE for fromJS means to wrap the C values at ap according to the
  274.  * format specifier and store them at sp, updating ap and sp appropriately.
  275.  *
  276.  * The "..." after "AA" is the rest of the format string that was passed into
  277.  * JS_{Convert,Push}Arguments{,VA}.  The actual format trailing substring used
  278.  * in each Convert or PushArguments call is passed to the formatter, so that
  279.  * one such function may implement several formats, in order to share code.
  280.  *
  281.  * Remove just forgets about any handler associated with format.  Add does not
  282.  * copy format, it points at the string storage allocated by the caller, which
  283.  * is typically a string constant.  If format is in dynamic storage, it is up
  284.  * to the caller to keep the string alive until Remove is called.
  285.  */
  286. extern JS_PUBLIC_API(JSBool)
  287. JS_AddArgumentFormatter(JSContext *cx, const char *format,
  288.                         JSArgumentFormatter formatter);
  289.  
  290. extern JS_PUBLIC_API(void)
  291. JS_RemoveArgumentFormatter(JSContext *cx, const char *format);
  292.  
  293. #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
  294.  
  295. extern JS_PUBLIC_API(JSBool)
  296. JS_ConvertValue(JSContext *cx, jsval v, JSType type, jsval *vp);
  297.  
  298. extern JS_PUBLIC_API(JSBool)
  299. JS_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
  300.  
  301. extern JS_PUBLIC_API(JSFunction *)
  302. JS_ValueToFunction(JSContext *cx, jsval v);
  303.  
  304. extern JS_PUBLIC_API(JSFunction *)
  305. JS_ValueToConstructor(JSContext *cx, jsval v);
  306.  
  307. extern JS_PUBLIC_API(JSString *)
  308. JS_ValueToString(JSContext *cx, jsval v);
  309.  
  310. extern JS_PUBLIC_API(JSBool)
  311. JS_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
  312.  
  313. /*
  314.  * Convert a value to a number, then to an int32, according to the ECMA rules
  315.  * for ToInt32.
  316.  */
  317. extern JS_PUBLIC_API(JSBool)
  318. JS_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip);
  319.  
  320. /*
  321.  * Convert a value to a number, then to a uint32, according to the ECMA rules
  322.  * for ToUint32.
  323.  */
  324. extern JS_PUBLIC_API(JSBool)
  325. JS_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip);
  326.  
  327. /*
  328.  * Convert a value to a number, then to an int32 if it fits by rounding to
  329.  * nearest; but failing with an error report if the double is out of range
  330.  * or unordered.
  331.  */
  332. extern JS_PUBLIC_API(JSBool)
  333. JS_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
  334.  
  335. /*
  336.  * ECMA ToUint16, for mapping a jsval to a Unicode point.
  337.  */
  338. extern JS_PUBLIC_API(JSBool)
  339. JS_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
  340.  
  341. extern JS_PUBLIC_API(JSBool)
  342. JS_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp);
  343.  
  344. extern JS_PUBLIC_API(JSType)
  345. JS_TypeOfValue(JSContext *cx, jsval v);
  346.  
  347. extern JS_PUBLIC_API(const char *)
  348. JS_GetTypeName(JSContext *cx, JSType type);
  349.  
  350. /************************************************************************/
  351.  
  352. /*
  353.  * Initialization, locking, contexts, and memory allocation.
  354.  */
  355. #define JS_NewRuntime       JS_Init
  356. #define JS_DestroyRuntime   JS_Finish
  357. #define JS_LockRuntime      JS_Lock
  358. #define JS_UnlockRuntime    JS_Unlock
  359.  
  360. extern JS_PUBLIC_API(JSRuntime *)
  361. JS_NewRuntime(uint32 maxbytes);
  362.  
  363. extern JS_PUBLIC_API(void)
  364. JS_DestroyRuntime(JSRuntime *rt);
  365.  
  366. extern JS_PUBLIC_API(void)
  367. JS_ShutDown(void);
  368.  
  369. JS_PUBLIC_API(void *)
  370. JS_GetRuntimePrivate(JSRuntime *rt);
  371.  
  372. JS_PUBLIC_API(void)
  373. JS_SetRuntimePrivate(JSRuntime *rt, void *data);
  374.  
  375. #ifdef JS_THREADSAFE
  376.  
  377. extern JS_PUBLIC_API(void)
  378. JS_BeginRequest(JSContext *cx);
  379.  
  380. extern JS_PUBLIC_API(void)
  381. JS_EndRequest(JSContext *cx);
  382.  
  383. /* Yield to pending GC operations, regardless of request depth */
  384. extern JS_PUBLIC_API(void)
  385. JS_YieldRequest(JSContext *cx);
  386.  
  387. extern JS_PUBLIC_API(jsrefcount)
  388. JS_SuspendRequest(JSContext *cx);
  389.  
  390. extern JS_PUBLIC_API(void)
  391. JS_ResumeRequest(JSContext *cx, jsrefcount saveDepth);
  392.  
  393. #endif /* JS_THREADSAFE */
  394.  
  395. extern JS_PUBLIC_API(void)
  396. JS_Lock(JSRuntime *rt);
  397.  
  398. extern JS_PUBLIC_API(void)
  399. JS_Unlock(JSRuntime *rt);
  400.  
  401. extern JS_PUBLIC_API(JSContext *)
  402. JS_NewContext(JSRuntime *rt, size_t stackChunkSize);
  403.  
  404. extern JS_PUBLIC_API(void)
  405. JS_DestroyContext(JSContext *cx);
  406.  
  407. extern JS_PUBLIC_API(void)
  408. JS_DestroyContextNoGC(JSContext *cx);
  409.  
  410. extern JS_PUBLIC_API(void)
  411. JS_DestroyContextMaybeGC(JSContext *cx);
  412.  
  413. extern JS_PUBLIC_API(void *)
  414. JS_GetContextPrivate(JSContext *cx);
  415.  
  416. extern JS_PUBLIC_API(void)
  417. JS_SetContextPrivate(JSContext *cx, void *data);
  418.  
  419. extern JS_PUBLIC_API(JSRuntime *)
  420. JS_GetRuntime(JSContext *cx);
  421.  
  422. extern JS_PUBLIC_API(JSContext *)
  423. JS_ContextIterator(JSRuntime *rt, JSContext **iterp);
  424.  
  425. extern JS_PUBLIC_API(JSVersion)
  426. JS_GetVersion(JSContext *cx);
  427.  
  428. extern JS_PUBLIC_API(JSVersion)
  429. JS_SetVersion(JSContext *cx, JSVersion version);
  430.  
  431. extern JS_PUBLIC_API(const char *)
  432. JS_VersionToString(JSVersion version);
  433.  
  434. extern JS_PUBLIC_API(JSVersion)
  435. JS_StringToVersion(const char *string);
  436.  
  437. /*
  438.  * JS options are orthogonal to version, and may be freely composed with one
  439.  * another as well as with version.
  440.  *
  441.  * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
  442.  * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
  443.  */
  444. #define JSOPTION_STRICT         JS_BIT(0)       /* warn on dubious practice */
  445. #define JSOPTION_WERROR         JS_BIT(1)       /* convert warning to error */
  446. #define JSOPTION_VAROBJFIX      JS_BIT(2)       /* make JS_EvaluateScript use
  447.                                                    the last object on its 'obj'
  448.                                                    param's scope chain as the
  449.                                                    ECMA 'variables object' */
  450. #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
  451.                                 JS_BIT(3)       /* context private data points
  452.                                                    to an nsISupports subclass */
  453. #define JSOPTION_COMPILE_N_GO   JS_BIT(4)       /* caller of JS_Compile*Script
  454.                                                    promises to execute compiled
  455.                                                    script once only; enables
  456.                                                    compile-time scope chain
  457.                                                    resolution of consts. */
  458. #define JSOPTION_ATLINE         JS_BIT(5)       /* //@line number ["filename"]
  459.                                                    option supported for the
  460.                                                    XUL preprocessor and kindred
  461.                                                    beasts. */
  462. #define JSOPTION_XML            JS_BIT(6)       /* EMCAScript for XML support:
  463.                                                    parse <!-- --> as a token,
  464.                                                    not backward compatible with
  465.                                                    the comment-hiding hack used
  466.                                                    in HTML script tags. */
  467. #define JSOPTION_NATIVE_BRANCH_CALLBACK \
  468.                                 JS_BIT(7)       /* the branch callback set by
  469.                                                    JS_SetBranchCallback may be
  470.                                                    called with a null script
  471.                                                    parameter, by native code
  472.                                                    that loops intensively */
  473.  
  474. extern JS_PUBLIC_API(uint32)
  475. JS_GetOptions(JSContext *cx);
  476.  
  477. extern JS_PUBLIC_API(uint32)
  478. JS_SetOptions(JSContext *cx, uint32 options);
  479.  
  480. extern JS_PUBLIC_API(uint32)
  481. JS_ToggleOptions(JSContext *cx, uint32 options);
  482.  
  483. extern JS_PUBLIC_API(const char *)
  484. JS_GetImplementationVersion(void);
  485.  
  486. extern JS_PUBLIC_API(JSObject *)
  487. JS_GetGlobalObject(JSContext *cx);
  488.  
  489. extern JS_PUBLIC_API(void)
  490. JS_SetGlobalObject(JSContext *cx, JSObject *obj);
  491.  
  492. /*
  493.  * Initialize standard JS class constructors, prototypes, and any top-level
  494.  * functions and constants associated with the standard classes (e.g. isNaN
  495.  * for Number).
  496.  *
  497.  * NB: This sets cx's global object to obj if it was null.
  498.  */
  499. extern JS_PUBLIC_API(JSBool)
  500. JS_InitStandardClasses(JSContext *cx, JSObject *obj);
  501.  
  502. /*
  503.  * Resolve id, which must contain either a string or an int, to a standard
  504.  * class name in obj if possible, defining the class's constructor and/or
  505.  * prototype and storing true in *resolved.  If id does not name a standard
  506.  * class or a top-level property induced by initializing a standard class,
  507.  * store false in *resolved and just return true.  Return false on error,
  508.  * as usual for JSBool result-typed API entry points.
  509.  *
  510.  * This API can be called directly from a global object class's resolve op,
  511.  * to define standard classes lazily.  The class's enumerate op should call
  512.  * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
  513.  * loops any classes not yet resolved lazily.
  514.  */
  515. extern JS_PUBLIC_API(JSBool)
  516. JS_ResolveStandardClass(JSContext *cx, JSObject *obj, jsval id,
  517.                         JSBool *resolved);
  518.  
  519. extern JS_PUBLIC_API(JSBool)
  520. JS_EnumerateStandardClasses(JSContext *cx, JSObject *obj);
  521.  
  522. /*
  523.  * Enumerate any already-resolved standard class ids into ida, or into a new
  524.  * JSIdArray if ida is null.  Return the augmented array on success, null on
  525.  * failure with ida (if it was non-null on entry) destroyed.
  526.  */
  527. extern JS_PUBLIC_API(JSIdArray *)
  528. JS_EnumerateResolvedStandardClasses(JSContext *cx, JSObject *obj,
  529.                                     JSIdArray *ida);
  530.  
  531. extern JS_PUBLIC_API(JSObject *)
  532. JS_GetScopeChain(JSContext *cx);
  533.  
  534. extern JS_PUBLIC_API(void *)
  535. JS_malloc(JSContext *cx, size_t nbytes);
  536.  
  537. extern JS_PUBLIC_API(void *)
  538. JS_realloc(JSContext *cx, void *p, size_t nbytes);
  539.  
  540. extern JS_PUBLIC_API(void)
  541. JS_free(JSContext *cx, void *p);
  542.  
  543. extern JS_PUBLIC_API(char *)
  544. JS_strdup(JSContext *cx, const char *s);
  545.  
  546. extern JS_PUBLIC_API(jsdouble *)
  547. JS_NewDouble(JSContext *cx, jsdouble d);
  548.  
  549. extern JS_PUBLIC_API(JSBool)
  550. JS_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
  551.  
  552. extern JS_PUBLIC_API(JSBool)
  553. JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
  554.  
  555. /*
  556.  * A JS GC root is a pointer to a JSObject *, JSString *, or jsdouble * that
  557.  * itself points into the GC heap (more recently, we support this extension:
  558.  * a root may be a pointer to a jsval v for which JSVAL_IS_GCTHING(v) is true).
  559.  *
  560.  * Therefore, you never pass JSObject *obj to JS_AddRoot(cx, obj).  You always
  561.  * call JS_AddRoot(cx, &obj), passing obj by reference.  And later, before obj
  562.  * or the structure it is embedded within goes out of scope or is freed, you
  563.  * must call JS_RemoveRoot(cx, &obj).
  564.  *
  565.  * Also, use JS_AddNamedRoot(cx, &structPtr->memberObj, "structPtr->memberObj")
  566.  * in preference to JS_AddRoot(cx, &structPtr->memberObj), in order to identify
  567.  * roots by their source callsites.  This way, you can find the callsite while
  568.  * debugging if you should fail to do JS_RemoveRoot(cx, &structPtr->memberObj)
  569.  * before freeing structPtr's memory.
  570.  */
  571. extern JS_PUBLIC_API(JSBool)
  572. JS_AddRoot(JSContext *cx, void *rp);
  573.  
  574. #ifdef NAME_ALL_GC_ROOTS
  575. #define JS_DEFINE_TO_TOKEN(def) #def
  576. #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
  577. #define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
  578. #endif
  579.  
  580. extern JS_PUBLIC_API(JSBool)
  581. JS_AddNamedRoot(JSContext *cx, void *rp, const char *name);
  582.  
  583. extern JS_PUBLIC_API(JSBool)
  584. JS_AddNamedRootRT(JSRuntime *rt, void *rp, const char *name);
  585.  
  586. extern JS_PUBLIC_API(JSBool)
  587. JS_RemoveRoot(JSContext *cx, void *rp);
  588.  
  589. extern JS_PUBLIC_API(JSBool)
  590. JS_RemoveRootRT(JSRuntime *rt, void *rp);
  591.  
  592. /*
  593.  * The last GC thing of each type (object, string, double, external string
  594.  * types) created on a given context is kept alive until another thing of the
  595.  * same type is created, using a newborn root in the context.  These newborn
  596.  * roots help native code protect newly-created GC-things from GC invocations
  597.  * activated before those things can be rooted using local or global roots.
  598.  *
  599.  * However, the newborn roots can also entrain great gobs of garbage, so the
  600.  * JS_GC entry point clears them for the context on which GC is being forced.
  601.  * Embeddings may need to do likewise for all contexts.
  602.  *
  603.  * See the scoped local root API immediately below for a better way to manage
  604.  * newborns in cases where native hooks (functions, getters, setters, etc.)
  605.  * create many GC-things, potentially without connecting them to predefined
  606.  * local roots such as *rval or argv[i] in an active native function.  Using
  607.  * JS_EnterLocalRootScope disables updating of the context's per-gc-thing-type
  608.  * newborn roots, until control flow unwinds and leaves the outermost nesting
  609.  * local root scope.
  610.  */
  611. extern JS_PUBLIC_API(void)
  612. JS_ClearNewbornRoots(JSContext *cx);
  613.  
  614. /*
  615.  * Scoped local root management allows native functions, getter/setters, etc.
  616.  * to avoid worrying about the newborn root pigeon-holes, overloading local
  617.  * roots allocated in argv and *rval, or ending up having to call JS_Add*Root
  618.  * and JS_RemoveRoot to manage global roots temporarily.
  619.  *
  620.  * Instead, calling JS_EnterLocalRootScope and JS_LeaveLocalRootScope around
  621.  * the body of the native hook causes the engine to allocate a local root for
  622.  * each newborn created in between the two API calls, using a local root stack
  623.  * associated with cx.  For example:
  624.  *
  625.  *    JSBool
  626.  *    my_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  627.  *    {
  628.  *        JSBool ok;
  629.  *
  630.  *        if (!JS_EnterLocalRootScope(cx))
  631.  *            return JS_FALSE;
  632.  *        ok = my_GetPropertyBody(cx, obj, id, vp);
  633.  *        JS_LeaveLocalRootScope(cx);
  634.  *        return ok;
  635.  *    }
  636.  *
  637.  * NB: JS_LeaveLocalRootScope must be called once for every prior successful
  638.  * call to JS_EnterLocalRootScope.  If JS_EnterLocalRootScope fails, you must
  639.  * not make the matching JS_LeaveLocalRootScope call.
  640.  *
  641.  * In case a native hook allocates many objects or other GC-things, but the
  642.  * native protects some of those GC-things by storing them as property values
  643.  * in an object that is itself protected, the hook can call JS_ForgetLocalRoot
  644.  * to free the local root automatically pushed for the now-protected GC-thing.
  645.  *
  646.  * JS_ForgetLocalRoot works on any GC-thing allocated in the current local
  647.  * root scope, but it's more time-efficient when called on references to more
  648.  * recently created GC-things.  Calling it successively on other than the most
  649.  * recently allocated GC-thing will tend to average the time inefficiency, and
  650.  * may risk O(n^2) growth rate, but in any event, you shouldn't allocate too
  651.  * many local roots if you can root as you go (build a tree of objects from
  652.  * the top down, forgetting each latest-allocated GC-thing immediately upon
  653.  * linking it to its parent).
  654.  */
  655. extern JS_PUBLIC_API(JSBool)
  656. JS_EnterLocalRootScope(JSContext *cx);
  657.  
  658. extern JS_PUBLIC_API(void)
  659. JS_LeaveLocalRootScope(JSContext *cx);
  660.  
  661. extern JS_PUBLIC_API(void)
  662. JS_ForgetLocalRoot(JSContext *cx, void *thing);
  663.  
  664. #ifdef DEBUG
  665. extern JS_PUBLIC_API(void)
  666. JS_DumpNamedRoots(JSRuntime *rt,
  667.                   void (*dump)(const char *name, void *rp, void *data),
  668.                   void *data);
  669. #endif
  670.  
  671. /*
  672.  * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
  673.  * The root is pointed at by rp; if the root is unnamed, name is null; data is
  674.  * supplied from the third parameter to JS_MapGCRoots.
  675.  *
  676.  * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
  677.  * enumerated root to be removed.  To stop enumeration, set JS_MAP_GCROOT_STOP
  678.  * in the return value.  To keep on mapping, return JS_MAP_GCROOT_NEXT.  These
  679.  * constants are flags; you can OR them together.
  680.  *
  681.  * This function acquires and releases rt's GC lock around the mapping of the
  682.  * roots table, so the map function should run to completion in as few cycles
  683.  * as possible.  Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
  684.  * or any JS API entry point that acquires locks, without double-tripping or
  685.  * deadlocking on the GC lock.
  686.  *
  687.  * JS_MapGCRoots returns the count of roots that were successfully mapped.
  688.  */
  689. #define JS_MAP_GCROOT_NEXT      0       /* continue mapping entries */
  690. #define JS_MAP_GCROOT_STOP      1       /* stop mapping entries */
  691. #define JS_MAP_GCROOT_REMOVE    2       /* remove and free the current entry */
  692.  
  693. typedef intN
  694. (* JS_DLL_CALLBACK JSGCRootMapFun)(void *rp, const char *name, void *data);
  695.  
  696. extern JS_PUBLIC_API(uint32)
  697. JS_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data);
  698.  
  699. extern JS_PUBLIC_API(JSBool)
  700. JS_LockGCThing(JSContext *cx, void *thing);
  701.  
  702. extern JS_PUBLIC_API(JSBool)
  703. JS_LockGCThingRT(JSRuntime *rt, void *thing);
  704.  
  705. extern JS_PUBLIC_API(JSBool)
  706. JS_UnlockGCThing(JSContext *cx, void *thing);
  707.  
  708. extern JS_PUBLIC_API(JSBool)
  709. JS_UnlockGCThingRT(JSRuntime *rt, void *thing);
  710.  
  711. /*
  712.  * For implementors of JSObjectOps.mark, to mark a GC-thing reachable via a
  713.  * property or other strong ref identified for debugging purposes by name.
  714.  * The name argument's storage needs to live only as long as the call to
  715.  * this routine.
  716.  *
  717.  * The final arg is used by GC_MARK_DEBUG code to build a ref path through
  718.  * the GC's live thing graph.  Implementors of JSObjectOps.mark should pass
  719.  * its final arg through to this function when marking all GC-things that are
  720.  * directly reachable from the object being marked.
  721.  *
  722.  * See the JSMarkOp typedef in jspubtd.h, and the JSObjectOps struct below.
  723.  */
  724. extern JS_PUBLIC_API(void)
  725. JS_MarkGCThing(JSContext *cx, void *thing, const char *name, void *arg);
  726.  
  727. extern JS_PUBLIC_API(void)
  728. JS_GC(JSContext *cx);
  729.  
  730. extern JS_PUBLIC_API(void)
  731. JS_MaybeGC(JSContext *cx);
  732.  
  733. extern JS_PUBLIC_API(JSGCCallback)
  734. JS_SetGCCallback(JSContext *cx, JSGCCallback cb);
  735.  
  736. extern JS_PUBLIC_API(JSGCCallback)
  737. JS_SetGCCallbackRT(JSRuntime *rt, JSGCCallback cb);
  738.  
  739. extern JS_PUBLIC_API(JSBool)
  740. JS_IsAboutToBeFinalized(JSContext *cx, void *thing);
  741.  
  742. typedef enum JSGCParamKey {
  743.     JSGC_MAX_BYTES        = 0,  /* maximum nominal heap before last ditch GC */
  744.     JSGC_MAX_MALLOC_BYTES = 1   /* # of JS_malloc bytes before last ditch GC */
  745. } JSGCParamKey;
  746.  
  747. extern JS_PUBLIC_API(void)
  748. JS_SetGCParameter(JSRuntime *rt, JSGCParamKey key, uint32 value);
  749.  
  750. /*
  751.  * Add a finalizer for external strings created by JS_NewExternalString (see
  752.  * below) using a type-code returned from this function, and that understands
  753.  * how to free or release the memory pointed at by JS_GetStringChars(str).
  754.  *
  755.  * Return a nonnegative type index if there is room for finalizer in the
  756.  * global GC finalizers table, else return -1.  If the engine is compiled
  757.  * JS_THREADSAFE and used in a multi-threaded environment, this function must
  758.  * be invoked on the primordial thread only, at startup -- or else the entire
  759.  * program must single-thread itself while loading a module that calls this
  760.  * function.
  761.  */
  762. extern JS_PUBLIC_API(intN)
  763. JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer);
  764.  
  765. /*
  766.  * Remove finalizer from the global GC finalizers table, returning its type
  767.  * code if found, -1 if not found.
  768.  *
  769.  * As with JS_AddExternalStringFinalizer, there is a threading restriction
  770.  * if you compile the engine JS_THREADSAFE: this function may be called for a
  771.  * given finalizer pointer on only one thread; different threads may call to
  772.  * remove distinct finalizers safely.
  773.  *
  774.  * You must ensure that all strings with finalizer's type have been collected
  775.  * before calling this function.  Otherwise, string data will be leaked by the
  776.  * GC, for want of a finalizer to call.
  777.  */
  778. extern JS_PUBLIC_API(intN)
  779. JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer);
  780.  
  781. /*
  782.  * Create a new JSString whose chars member refers to external memory, i.e.,
  783.  * memory requiring special, type-specific finalization.  The type code must
  784.  * be a nonnegative return value from JS_AddExternalStringFinalizer.
  785.  */
  786. extern JS_PUBLIC_API(JSString *)
  787. JS_NewExternalString(JSContext *cx, jschar *chars, size_t length, intN type);
  788.  
  789. /*
  790.  * Returns the external-string finalizer index for this string, or -1 if it is
  791.  * an "internal" (native to JS engine) string.
  792.  */
  793. extern JS_PUBLIC_API(intN)
  794. JS_GetExternalStringGCType(JSRuntime *rt, JSString *str);
  795.  
  796. /*
  797.  * Sets maximum (if stack grows upward) or minimum (downward) legal stack byte
  798.  * address in limitAddr for the thread or process stack used by cx.  To disable
  799.  * stack size checking, pass 0 for limitAddr.
  800.  */
  801. extern JS_PUBLIC_API(void)
  802. JS_SetThreadStackLimit(JSContext *cx, jsuword limitAddr);
  803.  
  804. /************************************************************************/
  805.  
  806. /*
  807.  * Classes, objects, and properties.
  808.  */
  809.  
  810. /* For detailed comments on the function pointer types, see jspubtd.h. */
  811. struct JSClass {
  812.     const char          *name;
  813.     uint32              flags;
  814.  
  815.     /* Mandatory non-null function pointer members. */
  816.     JSPropertyOp        addProperty;
  817.     JSPropertyOp        delProperty;
  818.     JSPropertyOp        getProperty;
  819.     JSPropertyOp        setProperty;
  820.     JSEnumerateOp       enumerate;
  821.     JSResolveOp         resolve;
  822.     JSConvertOp         convert;
  823.     JSFinalizeOp        finalize;
  824.  
  825.     /* Optionally non-null members start here. */
  826.     JSGetObjectOps      getObjectOps;
  827.     JSCheckAccessOp     checkAccess;
  828.     JSNative            call;
  829.     JSNative            construct;
  830.     JSXDRObjectOp       xdrObject;
  831.     JSHasInstanceOp     hasInstance;
  832.     JSMarkOp            mark;
  833.     JSReserveSlotsOp    reserveSlots;
  834. };
  835.  
  836. struct JSExtendedClass {
  837.     JSClass             base;
  838.     JSEqualityOp        equality;
  839.     JSObjectOp          outerObject;
  840.     JSObjectOp          innerObject;
  841.     jsword              reserved0;
  842.     jsword              reserved1;
  843.     jsword              reserved2;
  844.     jsword              reserved3;
  845.     jsword              reserved4;
  846. };
  847.  
  848. #define JSCLASS_HAS_PRIVATE             (1<<0)  /* objects have private slot */
  849. #define JSCLASS_NEW_ENUMERATE           (1<<1)  /* has JSNewEnumerateOp hook */
  850. #define JSCLASS_NEW_RESOLVE             (1<<2)  /* has JSNewResolveOp hook */
  851. #define JSCLASS_PRIVATE_IS_NSISUPPORTS  (1<<3)  /* private is (nsISupports *) */
  852. #define JSCLASS_SHARE_ALL_PROPERTIES    (1<<4)  /* all properties are SHARED */
  853. #define JSCLASS_NEW_RESOLVE_GETS_START  (1<<5)  /* JSNewResolveOp gets starting
  854.                                                    object in prototype chain
  855.                                                    passed in via *objp in/out
  856.                                                    parameter */
  857. #define JSCLASS_CONSTRUCT_PROTOTYPE     (1<<6)  /* call constructor on class
  858.                                                    prototype */
  859. #define JSCLASS_DOCUMENT_OBSERVER       (1<<7)  /* DOM document observer */
  860.  
  861. /*
  862.  * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
  863.  * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
  864.  * n is a constant in [1, 255].  Reserved slots are indexed from 0 to n-1.
  865.  */
  866. #define JSCLASS_RESERVED_SLOTS_SHIFT    8       /* room for 8 flags below */
  867. #define JSCLASS_RESERVED_SLOTS_WIDTH    8       /* and 16 above this field */
  868. #define JSCLASS_RESERVED_SLOTS_MASK     JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
  869. #define JSCLASS_HAS_RESERVED_SLOTS(n)   (((n) & JSCLASS_RESERVED_SLOTS_MASK)  \
  870.                                          << JSCLASS_RESERVED_SLOTS_SHIFT)
  871. #define JSCLASS_RESERVED_SLOTS(clasp)   (((clasp)->flags                      \
  872.                                           >> JSCLASS_RESERVED_SLOTS_SHIFT)    \
  873.                                          & JSCLASS_RESERVED_SLOTS_MASK)
  874.  
  875. #define JSCLASS_HIGH_FLAGS_SHIFT        (JSCLASS_RESERVED_SLOTS_SHIFT +       \
  876.                                          JSCLASS_RESERVED_SLOTS_WIDTH)
  877.  
  878. /* True if JSClass is really a JSExtendedClass. */
  879. #define JSCLASS_IS_EXTENDED             (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
  880.  
  881. /* Initializer for unused members of statically initialized JSClass structs. */
  882. #define JSCLASS_NO_OPTIONAL_MEMBERS     0,0,0,0,0,0,0,0
  883. #define JSCLASS_NO_RESERVED_MEMBERS     0,0,0,0,0
  884.  
  885. /* For detailed comments on these function pointer types, see jspubtd.h. */
  886. struct JSObjectOps {
  887.     /* Mandatory non-null function pointer members. */
  888.     JSNewObjectMapOp    newObjectMap;
  889.     JSObjectMapOp       destroyObjectMap;
  890.     JSLookupPropOp      lookupProperty;
  891.     JSDefinePropOp      defineProperty;
  892.     JSPropertyIdOp      getProperty;
  893.     JSPropertyIdOp      setProperty;
  894.     JSAttributesOp      getAttributes;
  895.     JSAttributesOp      setAttributes;
  896.     JSPropertyIdOp      deleteProperty;
  897.     JSConvertOp         defaultValue;
  898.     JSNewEnumerateOp    enumerate;
  899.     JSCheckAccessIdOp   checkAccess;
  900.  
  901.     /* Optionally non-null members start here. */
  902.     JSObjectOp          thisObject;
  903.     JSPropertyRefOp     dropProperty;
  904.     JSNative            call;
  905.     JSNative            construct;
  906.     JSXDRObjectOp       xdrObject;
  907.     JSHasInstanceOp     hasInstance;
  908.     JSSetObjectSlotOp   setProto;
  909.     JSSetObjectSlotOp   setParent;
  910.     JSMarkOp            mark;
  911.     JSFinalizeOp        clear;
  912.     JSGetRequiredSlotOp getRequiredSlot;
  913.     JSSetRequiredSlotOp setRequiredSlot;
  914. };
  915.  
  916. struct JSXMLObjectOps {
  917.     JSObjectOps         base;
  918.     JSGetMethodOp       getMethod;
  919.     JSSetMethodOp       setMethod;
  920.     JSEnumerateValuesOp enumerateValues;
  921.     JSEqualityOp        equality;
  922.     JSConcatenateOp     concatenate;
  923. };
  924.  
  925. /*
  926.  * Classes that expose JSObjectOps via a non-null getObjectOps class hook may
  927.  * derive a property structure from this struct, return a pointer to it from
  928.  * lookupProperty and defineProperty, and use the pointer to avoid rehashing
  929.  * in getAttributes and setAttributes.
  930.  *
  931.  * The jsid type contains either an int jsval (see JSVAL_IS_INT above), or an
  932.  * internal pointer that is opaque to users of this API, but which users may
  933.  * convert from and to a jsval using JS_ValueToId and JS_IdToValue.
  934.  */
  935. struct JSProperty {
  936.     jsid id;
  937. };
  938.  
  939. struct JSIdArray {
  940.     jsint length;
  941.     jsid  vector[1];    /* actually, length jsid words */
  942. };
  943.  
  944. extern JS_PUBLIC_API(void)
  945. JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
  946.  
  947. extern JS_PUBLIC_API(JSBool)
  948. JS_ValueToId(JSContext *cx, jsval v, jsid *idp);
  949.  
  950. extern JS_PUBLIC_API(JSBool)
  951. JS_IdToValue(JSContext *cx, jsid id, jsval *vp);
  952.  
  953. /*
  954.  * The magic XML namespace id is int-tagged, but not a valid integer jsval.
  955.  * Global object classes in embeddings that enable JS_HAS_XML_SUPPORT (E4X)
  956.  * should handle this id specially before converting id via JSVAL_TO_INT.
  957.  */
  958. #define JS_DEFAULT_XML_NAMESPACE_ID ((jsid) JSVAL_VOID)
  959.  
  960. /*
  961.  * JSNewResolveOp flag bits.
  962.  */
  963. #define JSRESOLVE_QUALIFIED     0x01    /* resolve a qualified property id */
  964. #define JSRESOLVE_ASSIGNING     0x02    /* resolve on the left of assignment */
  965. #define JSRESOLVE_DETECTING     0x04    /* 'if (o.p)...' or '(o.p) ?...:...' */
  966. #define JSRESOLVE_DECLARING     0x08    /* var, const, or function prolog op */
  967. #define JSRESOLVE_CLASSNAME     0x10    /* class name used when constructing */
  968.  
  969. extern JS_PUBLIC_API(JSBool)
  970. JS_PropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
  971.  
  972. extern JS_PUBLIC_API(JSBool)
  973. JS_EnumerateStub(JSContext *cx, JSObject *obj);
  974.  
  975. extern JS_PUBLIC_API(JSBool)
  976. JS_ResolveStub(JSContext *cx, JSObject *obj, jsval id);
  977.  
  978. extern JS_PUBLIC_API(JSBool)
  979. JS_ConvertStub(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
  980.  
  981. extern JS_PUBLIC_API(void)
  982. JS_FinalizeStub(JSContext *cx, JSObject *obj);
  983.  
  984. struct JSConstDoubleSpec {
  985.     jsdouble        dval;
  986.     const char      *name;
  987.     uint8           flags;
  988.     uint8           spare[3];
  989. };
  990.  
  991. /*
  992.  * To define an array element rather than a named property member, cast the
  993.  * element's index to (const char *) and initialize name with it, and set the
  994.  * JSPROP_INDEX bit in flags.
  995.  */
  996. struct JSPropertySpec {
  997.     const char      *name;
  998.     int8            tinyid;
  999.     uint8           flags;
  1000.     JSPropertyOp    getter;
  1001.     JSPropertyOp    setter;
  1002. };
  1003.  
  1004. struct JSFunctionSpec {
  1005.     const char      *name;
  1006.     JSNative        call;
  1007.     uint8           nargs;
  1008.     uint8           flags;
  1009.     uint16          extra;      /* number of arg slots for local GC roots */
  1010. };
  1011.  
  1012. extern JS_PUBLIC_API(JSObject *)
  1013. JS_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
  1014.              JSClass *clasp, JSNative constructor, uintN nargs,
  1015.              JSPropertySpec *ps, JSFunctionSpec *fs,
  1016.              JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
  1017.  
  1018. #ifdef JS_THREADSAFE
  1019. extern JS_PUBLIC_API(JSClass *)
  1020. JS_GetClass(JSContext *cx, JSObject *obj);
  1021.  
  1022. #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
  1023. #else
  1024. extern JS_PUBLIC_API(JSClass *)
  1025. JS_GetClass(JSObject *obj);
  1026.  
  1027. #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
  1028. #endif
  1029.  
  1030. extern JS_PUBLIC_API(JSBool)
  1031. JS_InstanceOf(JSContext *cx, JSObject *obj, JSClass *clasp, jsval *argv);
  1032.  
  1033. extern JS_PUBLIC_API(JSBool)
  1034. JS_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
  1035.  
  1036. extern JS_PUBLIC_API(void *)
  1037. JS_GetPrivate(JSContext *cx, JSObject *obj);
  1038.  
  1039. extern JS_PUBLIC_API(JSBool)
  1040. JS_SetPrivate(JSContext *cx, JSObject *obj, void *data);
  1041.  
  1042. extern JS_PUBLIC_API(void *)
  1043. JS_GetInstancePrivate(JSContext *cx, JSObject *obj, JSClass *clasp,
  1044.                       jsval *argv);
  1045.  
  1046. extern JS_PUBLIC_API(JSObject *)
  1047. JS_GetPrototype(JSContext *cx, JSObject *obj);
  1048.  
  1049. extern JS_PUBLIC_API(JSBool)
  1050. JS_SetPrototype(JSContext *cx, JSObject *obj, JSObject *proto);
  1051.  
  1052. extern JS_PUBLIC_API(JSObject *)
  1053. JS_GetParent(JSContext *cx, JSObject *obj);
  1054.  
  1055. extern JS_PUBLIC_API(JSBool)
  1056. JS_SetParent(JSContext *cx, JSObject *obj, JSObject *parent);
  1057.  
  1058. extern JS_PUBLIC_API(JSObject *)
  1059. JS_GetConstructor(JSContext *cx, JSObject *proto);
  1060.  
  1061. /*
  1062.  * Get a unique identifier for obj, good for the lifetime of obj (even if it
  1063.  * is moved by a copying GC).  Return false on failure (likely out of memory),
  1064.  * and true with *idp containing the unique id on success.
  1065.  */
  1066. extern JS_PUBLIC_API(JSBool)
  1067. JS_GetObjectId(JSContext *cx, JSObject *obj, jsid *idp);
  1068.  
  1069. extern JS_PUBLIC_API(JSObject *)
  1070. JS_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent);
  1071.  
  1072. extern JS_PUBLIC_API(JSBool)
  1073. JS_SealObject(JSContext *cx, JSObject *obj, JSBool deep);
  1074.  
  1075. extern JS_PUBLIC_API(JSObject *)
  1076. JS_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
  1077.                    JSObject *parent);
  1078.  
  1079. extern JS_PUBLIC_API(JSObject *)
  1080. JS_ConstructObjectWithArguments(JSContext *cx, JSClass *clasp, JSObject *proto,
  1081.                                 JSObject *parent, uintN argc, jsval *argv);
  1082.  
  1083. extern JS_PUBLIC_API(JSObject *)
  1084. JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, JSClass *clasp,
  1085.                 JSObject *proto, uintN attrs);
  1086.  
  1087. extern JS_PUBLIC_API(JSBool)
  1088. JS_DefineConstDoubles(JSContext *cx, JSObject *obj, JSConstDoubleSpec *cds);
  1089.  
  1090. extern JS_PUBLIC_API(JSBool)
  1091. JS_DefineProperties(JSContext *cx, JSObject *obj, JSPropertySpec *ps);
  1092.  
  1093. extern JS_PUBLIC_API(JSBool)
  1094. JS_DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
  1095.                   JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
  1096.  
  1097. /*
  1098.  * Determine the attributes (JSPROP_* flags) of a property on a given object.
  1099.  *
  1100.  * If the object does not have a property by that name, *foundp will be
  1101.  * JS_FALSE and the value of *attrsp is undefined.
  1102.  */
  1103. extern JS_PUBLIC_API(JSBool)
  1104. JS_GetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
  1105.                          uintN *attrsp, JSBool *foundp);
  1106.  
  1107. /*
  1108.  * The same, but if the property is native, return its getter and setter via
  1109.  * *getterp and *setterp, respectively (and only if the out parameter pointer
  1110.  * is not null).
  1111.  */
  1112. extern JS_PUBLIC_API(JSBool)
  1113. JS_GetPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
  1114.                                    const char *name,
  1115.                                    uintN *attrsp, JSBool *foundp,
  1116.                                    JSPropertyOp *getterp,
  1117.                                    JSPropertyOp *setterp);
  1118.  
  1119. /*
  1120.  * Set the attributes of a property on a given object.
  1121.  *
  1122.  * If the object does not have a property by that name, *foundp will be
  1123.  * JS_FALSE and nothing will be altered.
  1124.  */
  1125. extern JS_PUBLIC_API(JSBool)
  1126. JS_SetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
  1127.                          uintN attrs, JSBool *foundp);
  1128.  
  1129. extern JS_PUBLIC_API(JSBool)
  1130. JS_DefinePropertyWithTinyId(JSContext *cx, JSObject *obj, const char *name,
  1131.                             int8 tinyid, jsval value,
  1132.                             JSPropertyOp getter, JSPropertyOp setter,
  1133.                             uintN attrs);
  1134.  
  1135. extern JS_PUBLIC_API(JSBool)
  1136. JS_AliasProperty(JSContext *cx, JSObject *obj, const char *name,
  1137.                  const char *alias);
  1138.  
  1139. extern JS_PUBLIC_API(JSBool)
  1140. JS_HasProperty(JSContext *cx, JSObject *obj, const char *name, JSBool *foundp);
  1141.  
  1142. extern JS_PUBLIC_API(JSBool)
  1143. JS_LookupProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
  1144.  
  1145. extern JS_PUBLIC_API(JSBool)
  1146. JS_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, const char *name,
  1147.                            uintN flags, jsval *vp);
  1148.  
  1149. extern JS_PUBLIC_API(JSBool)
  1150. JS_GetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
  1151.  
  1152. extern JS_PUBLIC_API(JSBool)
  1153. JS_GetMethod(JSContext *cx, JSObject *obj, const char *name, JSObject **objp,
  1154.              jsval *vp);
  1155.  
  1156. extern JS_PUBLIC_API(JSBool)
  1157. JS_SetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
  1158.  
  1159. extern JS_PUBLIC_API(JSBool)
  1160. JS_DeleteProperty(JSContext *cx, JSObject *obj, const char *name);
  1161.  
  1162. extern JS_PUBLIC_API(JSBool)
  1163. JS_DeleteProperty2(JSContext *cx, JSObject *obj, const char *name,
  1164.                    jsval *rval);
  1165.  
  1166. extern JS_PUBLIC_API(JSBool)
  1167. JS_DefineUCProperty(JSContext *cx, JSObject *obj,
  1168.                     const jschar *name, size_t namelen, jsval value,
  1169.                     JSPropertyOp getter, JSPropertyOp setter,
  1170.                     uintN attrs);
  1171.  
  1172. /*
  1173.  * Determine the attributes (JSPROP_* flags) of a property on a given object.
  1174.  *
  1175.  * If the object does not have a property by that name, *foundp will be
  1176.  * JS_FALSE and the value of *attrsp is undefined.
  1177.  */
  1178. extern JS_PUBLIC_API(JSBool)
  1179. JS_GetUCPropertyAttributes(JSContext *cx, JSObject *obj,
  1180.                            const jschar *name, size_t namelen,
  1181.                            uintN *attrsp, JSBool *foundp);
  1182.  
  1183. /*
  1184.  * The same, but if the property is native, return its getter and setter via
  1185.  * *getterp and *setterp, respectively (and only if the out parameter pointer
  1186.  * is not null).
  1187.  */
  1188. extern JS_PUBLIC_API(JSBool)
  1189. JS_GetUCPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
  1190.                                      const jschar *name, size_t namelen,
  1191.                                      uintN *attrsp, JSBool *foundp,
  1192.                                      JSPropertyOp *getterp,
  1193.                                      JSPropertyOp *setterp);
  1194.  
  1195. /*
  1196.  * Set the attributes of a property on a given object.
  1197.  *
  1198.  * If the object does not have a property by that name, *foundp will be
  1199.  * JS_FALSE and nothing will be altered.
  1200.  */
  1201. extern JS_PUBLIC_API(JSBool)
  1202. JS_SetUCPropertyAttributes(JSContext *cx, JSObject *obj,
  1203.                            const jschar *name, size_t namelen,
  1204.                            uintN attrs, JSBool *foundp);
  1205.  
  1206.  
  1207. extern JS_PUBLIC_API(JSBool)
  1208. JS_DefineUCPropertyWithTinyId(JSContext *cx, JSObject *obj,
  1209.                               const jschar *name, size_t namelen,
  1210.                               int8 tinyid, jsval value,
  1211.                               JSPropertyOp getter, JSPropertyOp setter,
  1212.                               uintN attrs);
  1213.  
  1214. extern JS_PUBLIC_API(JSBool)
  1215. JS_HasUCProperty(JSContext *cx, JSObject *obj,
  1216.                  const jschar *name, size_t namelen,
  1217.                  JSBool *vp);
  1218.  
  1219. extern JS_PUBLIC_API(JSBool)
  1220. JS_LookupUCProperty(JSContext *cx, JSObject *obj,
  1221.                     const jschar *name, size_t namelen,
  1222.                     jsval *vp);
  1223.  
  1224. extern JS_PUBLIC_API(JSBool)
  1225. JS_GetUCProperty(JSContext *cx, JSObject *obj,
  1226.                  const jschar *name, size_t namelen,
  1227.                  jsval *vp);
  1228.  
  1229. extern JS_PUBLIC_API(JSBool)
  1230. JS_SetUCProperty(JSContext *cx, JSObject *obj,
  1231.                  const jschar *name, size_t namelen,
  1232.                  jsval *vp);
  1233.  
  1234. extern JS_PUBLIC_API(JSBool)
  1235. JS_DeleteUCProperty2(JSContext *cx, JSObject *obj,
  1236.                      const jschar *name, size_t namelen,
  1237.                      jsval *rval);
  1238.  
  1239. extern JS_PUBLIC_API(JSObject *)
  1240. JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector);
  1241.  
  1242. extern JS_PUBLIC_API(JSBool)
  1243. JS_IsArrayObject(JSContext *cx, JSObject *obj);
  1244.  
  1245. extern JS_PUBLIC_API(JSBool)
  1246. JS_GetArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
  1247.  
  1248. extern JS_PUBLIC_API(JSBool)
  1249. JS_SetArrayLength(JSContext *cx, JSObject *obj, jsuint length);
  1250.  
  1251. extern JS_PUBLIC_API(JSBool)
  1252. JS_HasArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
  1253.  
  1254. extern JS_PUBLIC_API(JSBool)
  1255. JS_DefineElement(JSContext *cx, JSObject *obj, jsint index, jsval value,
  1256.                  JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
  1257.  
  1258. extern JS_PUBLIC_API(JSBool)
  1259. JS_AliasElement(JSContext *cx, JSObject *obj, const char *name, jsint alias);
  1260.  
  1261. extern JS_PUBLIC_API(JSBool)
  1262. JS_HasElement(JSContext *cx, JSObject *obj, jsint index, JSBool *foundp);
  1263.  
  1264. extern JS_PUBLIC_API(JSBool)
  1265. JS_LookupElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
  1266.  
  1267. extern JS_PUBLIC_API(JSBool)
  1268. JS_GetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
  1269.  
  1270. extern JS_PUBLIC_API(JSBool)
  1271. JS_SetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
  1272.  
  1273. extern JS_PUBLIC_API(JSBool)
  1274. JS_DeleteElement(JSContext *cx, JSObject *obj, jsint index);
  1275.  
  1276. extern JS_PUBLIC_API(JSBool)
  1277. JS_DeleteElement2(JSContext *cx, JSObject *obj, jsint index, jsval *rval);
  1278.  
  1279. extern JS_PUBLIC_API(void)
  1280. JS_ClearScope(JSContext *cx, JSObject *obj);
  1281.  
  1282. extern JS_PUBLIC_API(JSIdArray *)
  1283. JS_Enumerate(JSContext *cx, JSObject *obj);
  1284.  
  1285. /*
  1286.  * Create an object to iterate over enumerable properties of obj, in arbitrary
  1287.  * property definition order.  NB: This differs from longstanding for..in loop
  1288.  * order, which uses order of property definition in obj.
  1289.  */
  1290. extern JS_PUBLIC_API(JSObject *)
  1291. JS_NewPropertyIterator(JSContext *cx, JSObject *obj);
  1292.  
  1293. /*
  1294.  * Return true on success with *idp containing the id of the next enumerable
  1295.  * property to visit using iterobj, or JSVAL_VOID if there is no such property
  1296.  * left to visit.  Return false on error.
  1297.  */
  1298. extern JS_PUBLIC_API(JSBool)
  1299. JS_NextProperty(JSContext *cx, JSObject *iterobj, jsid *idp);
  1300.  
  1301. extern JS_PUBLIC_API(JSBool)
  1302. JS_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
  1303.                jsval *vp, uintN *attrsp);
  1304.  
  1305. extern JS_PUBLIC_API(JSCheckAccessOp)
  1306. JS_SetCheckObjectAccessCallback(JSRuntime *rt, JSCheckAccessOp acb);
  1307.  
  1308. extern JS_PUBLIC_API(JSBool)
  1309. JS_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
  1310.  
  1311. extern JS_PUBLIC_API(JSBool)
  1312. JS_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
  1313.  
  1314. /************************************************************************/
  1315.  
  1316. /*
  1317.  * Security protocol.
  1318.  */
  1319. struct JSPrincipals {
  1320.     char *codebase;
  1321.  
  1322.     /* XXX unspecified and unused by Mozilla code -- can we remove these? */
  1323.     void * (* JS_DLL_CALLBACK getPrincipalArray)(JSContext *cx, JSPrincipals *);
  1324.     JSBool (* JS_DLL_CALLBACK globalPrivilegesEnabled)(JSContext *cx, JSPrincipals *);
  1325.  
  1326.     /* Don't call "destroy"; use reference counting macros below. */
  1327.     jsrefcount refcount;
  1328.  
  1329.     void   (* JS_DLL_CALLBACK destroy)(JSContext *cx, JSPrincipals *);
  1330.     JSBool (* JS_DLL_CALLBACK subsume)(JSPrincipals *, JSPrincipals *);
  1331. };
  1332.  
  1333. #ifdef JS_THREADSAFE
  1334. #define JSPRINCIPALS_HOLD(cx, principals)   JS_HoldPrincipals(cx,principals)
  1335. #define JSPRINCIPALS_DROP(cx, principals)   JS_DropPrincipals(cx,principals)
  1336.  
  1337. extern JS_PUBLIC_API(jsrefcount)
  1338. JS_HoldPrincipals(JSContext *cx, JSPrincipals *principals);
  1339.  
  1340. extern JS_PUBLIC_API(jsrefcount)
  1341. JS_DropPrincipals(JSContext *cx, JSPrincipals *principals);
  1342.  
  1343. #else
  1344. #define JSPRINCIPALS_HOLD(cx, principals)   (++(principals)->refcount)
  1345. #define JSPRINCIPALS_DROP(cx, principals)                                     \
  1346.     ((--(principals)->refcount == 0)                                          \
  1347.      ? ((*(principals)->destroy)((cx), (principals)), 0)                      \
  1348.      : (principals)->refcount)
  1349. #endif
  1350.  
  1351. extern JS_PUBLIC_API(JSPrincipalsTranscoder)
  1352. JS_SetPrincipalsTranscoder(JSRuntime *rt, JSPrincipalsTranscoder px);
  1353.  
  1354. extern JS_PUBLIC_API(JSObjectPrincipalsFinder)
  1355. JS_SetObjectPrincipalsFinder(JSRuntime *rt, JSObjectPrincipalsFinder fop);
  1356.  
  1357. /************************************************************************/
  1358.  
  1359. /*
  1360.  * Functions and scripts.
  1361.  */
  1362. extern JS_PUBLIC_API(JSFunction *)
  1363. JS_NewFunction(JSContext *cx, JSNative call, uintN nargs, uintN flags,
  1364.                JSObject *parent, const char *name);
  1365.  
  1366. extern JS_PUBLIC_API(JSObject *)
  1367. JS_GetFunctionObject(JSFunction *fun);
  1368.  
  1369. /*
  1370.  * Deprecated, useful only for diagnostics.  Use JS_GetFunctionId instead for
  1371.  * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
  1372.  */
  1373. extern JS_PUBLIC_API(const char *)
  1374. JS_GetFunctionName(JSFunction *fun);
  1375.  
  1376. /*
  1377.  * Return the function's identifier as a JSString, or null if fun is unnamed.
  1378.  * The returned string lives as long as fun, so you don't need to root a saved
  1379.  * reference to it if fun is well-connected or rooted, and provided you bound
  1380.  * the use of the saved reference by fun's lifetime.
  1381.  *
  1382.  * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
  1383.  * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
  1384.  * from UTF-16-ish jschars.
  1385.  */
  1386. extern JS_PUBLIC_API(JSString *)
  1387. JS_GetFunctionId(JSFunction *fun);
  1388.  
  1389. /*
  1390.  * Return JSFUN_* flags for fun.
  1391.  */
  1392. extern JS_PUBLIC_API(uintN)
  1393. JS_GetFunctionFlags(JSFunction *fun);
  1394.  
  1395. /*
  1396.  * Infallible predicate to test whether obj is a function object (faster than
  1397.  * comparing obj's class name to "Function", but equivalent unless someone has
  1398.  * overwritten the "Function" identifier with a different constructor and then
  1399.  * created instances using that constructor that might be passed in as obj).
  1400.  */
  1401. extern JS_PUBLIC_API(JSBool)
  1402. JS_ObjectIsFunction(JSContext *cx, JSObject *obj);
  1403.  
  1404. extern JS_PUBLIC_API(JSBool)
  1405. JS_DefineFunctions(JSContext *cx, JSObject *obj, JSFunctionSpec *fs);
  1406.  
  1407. extern JS_PUBLIC_API(JSFunction *)
  1408. JS_DefineFunction(JSContext *cx, JSObject *obj, const char *name, JSNative call,
  1409.                   uintN nargs, uintN attrs);
  1410.  
  1411. extern JS_PUBLIC_API(JSFunction *)
  1412. JS_DefineUCFunction(JSContext *cx, JSObject *obj,
  1413.                     const jschar *name, size_t namelen, JSNative call,
  1414.                     uintN nargs, uintN attrs);
  1415.  
  1416. extern JS_PUBLIC_API(JSObject *)
  1417. JS_CloneFunctionObject(JSContext *cx, JSObject *funobj, JSObject *parent);
  1418.  
  1419. /*
  1420.  * Given a buffer, return JS_FALSE if the buffer might become a valid
  1421.  * javascript statement with the addition of more lines.  Otherwise return
  1422.  * JS_TRUE.  The intent is to support interactive compilation - accumulate
  1423.  * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
  1424.  * the compiler.
  1425.  */
  1426. extern JS_PUBLIC_API(JSBool)
  1427. JS_BufferIsCompilableUnit(JSContext *cx, JSObject *obj,
  1428.                           const char *bytes, size_t length);
  1429.  
  1430. /*
  1431.  * The JSScript objects returned by the following functions refer to string and
  1432.  * other kinds of literals, including doubles and RegExp objects.  These
  1433.  * literals are vulnerable to garbage collection; to root script objects and
  1434.  * prevent literals from being collected, create a rootable object using
  1435.  * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
  1436.  */
  1437. extern JS_PUBLIC_API(JSScript *)
  1438. JS_CompileScript(JSContext *cx, JSObject *obj,
  1439.                  const char *bytes, size_t length,
  1440.                  const char *filename, uintN lineno);
  1441.  
  1442. extern JS_PUBLIC_API(JSScript *)
  1443. JS_CompileScriptForPrincipals(JSContext *cx, JSObject *obj,
  1444.                               JSPrincipals *principals,
  1445.                               const char *bytes, size_t length,
  1446.                               const char *filename, uintN lineno);
  1447.  
  1448. extern JS_PUBLIC_API(JSScript *)
  1449. JS_CompileUCScript(JSContext *cx, JSObject *obj,
  1450.                    const jschar *chars, size_t length,
  1451.                    const char *filename, uintN lineno);
  1452.  
  1453. extern JS_PUBLIC_API(JSScript *)
  1454. JS_CompileUCScriptForPrincipals(JSContext *cx, JSObject *obj,
  1455.                                 JSPrincipals *principals,
  1456.                                 const jschar *chars, size_t length,
  1457.                                 const char *filename, uintN lineno);
  1458.  
  1459. extern JS_PUBLIC_API(JSScript *)
  1460. JS_CompileFile(JSContext *cx, JSObject *obj, const char *filename);
  1461.  
  1462. extern JS_PUBLIC_API(JSScript *)
  1463. JS_CompileFileHandle(JSContext *cx, JSObject *obj, const char *filename,
  1464.                      FILE *fh);
  1465.  
  1466. extern JS_PUBLIC_API(JSScript *)
  1467. JS_CompileFileHandleForPrincipals(JSContext *cx, JSObject *obj,
  1468.                                   const char *filename, FILE *fh,
  1469.                                   JSPrincipals *principals);
  1470.  
  1471. /*
  1472.  * NB: you must use JS_NewScriptObject and root a pointer to its return value
  1473.  * in order to keep a JSScript and its atoms safe from garbage collection after
  1474.  * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
  1475.  * E.g., and without error checks:
  1476.  *
  1477.  *    JSScript *script = JS_CompileFile(cx, global, filename);
  1478.  *    JSObject *scrobj = JS_NewScriptObject(cx, script);
  1479.  *    JS_AddNamedRoot(cx, &scrobj, "scrobj");
  1480.  *    do {
  1481.  *        jsval result;
  1482.  *        JS_ExecuteScript(cx, global, script, &result);
  1483.  *        JS_GC();
  1484.  *    } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
  1485.  *    JS_RemoveRoot(cx, &scrobj);
  1486.  */
  1487. extern JS_PUBLIC_API(JSObject *)
  1488. JS_NewScriptObject(JSContext *cx, JSScript *script);
  1489.  
  1490. /*
  1491.  * Infallible getter for a script's object.  If JS_NewScriptObject has not been
  1492.  * called on script yet, the return value will be null.
  1493.  */
  1494. extern JS_PUBLIC_API(JSObject *)
  1495. JS_GetScriptObject(JSScript *script);
  1496.  
  1497. extern JS_PUBLIC_API(void)
  1498. JS_DestroyScript(JSContext *cx, JSScript *script);
  1499.  
  1500. extern JS_PUBLIC_API(JSFunction *)
  1501. JS_CompileFunction(JSContext *cx, JSObject *obj, const char *name,
  1502.                    uintN nargs, const char **argnames,
  1503.                    const char *bytes, size_t length,
  1504.                    const char *filename, uintN lineno);
  1505.  
  1506. extern JS_PUBLIC_API(JSFunction *)
  1507. JS_CompileFunctionForPrincipals(JSContext *cx, JSObject *obj,
  1508.                                 JSPrincipals *principals, const char *name,
  1509.                                 uintN nargs, const char **argnames,
  1510.                                 const char *bytes, size_t length,
  1511.                                 const char *filename, uintN lineno);
  1512.  
  1513. extern JS_PUBLIC_API(JSFunction *)
  1514. JS_CompileUCFunction(JSContext *cx, JSObject *obj, const char *name,
  1515.                      uintN nargs, const char **argnames,
  1516.                      const jschar *chars, size_t length,
  1517.                      const char *filename, uintN lineno);
  1518.  
  1519. extern JS_PUBLIC_API(JSFunction *)
  1520. JS_CompileUCFunctionForPrincipals(JSContext *cx, JSObject *obj,
  1521.                                   JSPrincipals *principals, const char *name,
  1522.                                   uintN nargs, const char **argnames,
  1523.                                   const jschar *chars, size_t length,
  1524.                                   const char *filename, uintN lineno);
  1525.  
  1526. extern JS_PUBLIC_API(JSString *)
  1527. JS_DecompileScript(JSContext *cx, JSScript *script, const char *name,
  1528.                    uintN indent);
  1529.  
  1530. /*
  1531.  * API extension: OR this into indent to avoid pretty-printing the decompiled
  1532.  * source resulting from JS_DecompileFunction{,Body}.
  1533.  */
  1534. #define JS_DONT_PRETTY_PRINT    ((uintN)0x8000)
  1535.  
  1536. extern JS_PUBLIC_API(JSString *)
  1537. JS_DecompileFunction(JSContext *cx, JSFunction *fun, uintN indent);
  1538.  
  1539. extern JS_PUBLIC_API(JSString *)
  1540. JS_DecompileFunctionBody(JSContext *cx, JSFunction *fun, uintN indent);
  1541.  
  1542. /*
  1543.  * NB: JS_ExecuteScript, JS_ExecuteScriptPart, and the JS_Evaluate*Script*
  1544.  * quadruplets all use the obj parameter as the initial scope chain header,
  1545.  * the 'this' keyword value, and the variables object (ECMA parlance for where
  1546.  * 'var' and 'function' bind names) of the execution context for script.
  1547.  *
  1548.  * Using obj as the variables object is problematic if obj's parent (which is
  1549.  * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
  1550.  * this case, variables created by 'var x = 0', e.g., go in obj, but variables
  1551.  * created by assignment to an unbound id, 'x = 0', go in the last object on
  1552.  * the scope chain linked by parent.
  1553.  *
  1554.  * ECMA calls that last scoping object the "global object", but note that many
  1555.  * embeddings have several such objects.  ECMA requires that "global code" be
  1556.  * executed with the variables object equal to this global object.  But these
  1557.  * JS API entry points provide freedom to execute code against a "sub-global",
  1558.  * i.e., a parented or scoped object, in which case the variables object will
  1559.  * differ from the last object on the scope chain, resulting in confusing and
  1560.  * non-ECMA explicit vs. implicit variable creation.
  1561.  *
  1562.  * Caveat embedders: unless you already depend on this buggy variables object
  1563.  * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
  1564.  * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
  1565.  * someone may have set other options on cx already -- for each context in the
  1566.  * application, if you pass parented objects as the obj parameter, or may ever
  1567.  * pass such objects in the future.
  1568.  *
  1569.  * Why a runtime option?  The alternative is to add six or so new API entry
  1570.  * points with signatures matching the following six, and that doesn't seem
  1571.  * worth the code bloat cost.  Such new entry points would probably have less
  1572.  * obvious names, too, so would not tend to be used.  The JS_SetOption call,
  1573.  * OTOH, can be more easily hacked into existing code that does not depend on
  1574.  * the bug; such code can continue to use the familiar JS_EvaluateScript,
  1575.  * etc., entry points.
  1576.  */
  1577. extern JS_PUBLIC_API(JSBool)
  1578. JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval);
  1579.  
  1580. /*
  1581.  * Execute either the function-defining prolog of a script, or the script's
  1582.  * main body, but not both.
  1583.  */
  1584. typedef enum JSExecPart { JSEXEC_PROLOG, JSEXEC_MAIN } JSExecPart;
  1585.  
  1586. extern JS_PUBLIC_API(JSBool)
  1587. JS_ExecuteScriptPart(JSContext *cx, JSObject *obj, JSScript *script,
  1588.                      JSExecPart part, jsval *rval);
  1589.  
  1590. extern JS_PUBLIC_API(JSBool)
  1591. JS_EvaluateScript(JSContext *cx, JSObject *obj,
  1592.                   const char *bytes, uintN length,
  1593.                   const char *filename, uintN lineno,
  1594.                   jsval *rval);
  1595.  
  1596. extern JS_PUBLIC_API(JSBool)
  1597. JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj,
  1598.                                JSPrincipals *principals,
  1599.                                const char *bytes, uintN length,
  1600.                                const char *filename, uintN lineno,
  1601.                                jsval *rval);
  1602.  
  1603. extern JS_PUBLIC_API(JSBool)
  1604. JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
  1605.                     const jschar *chars, uintN length,
  1606.                     const char *filename, uintN lineno,
  1607.                     jsval *rval);
  1608.  
  1609. extern JS_PUBLIC_API(JSBool)
  1610. JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj,
  1611.                                  JSPrincipals *principals,
  1612.                                  const jschar *chars, uintN length,
  1613.                                  const char *filename, uintN lineno,
  1614.                                  jsval *rval);
  1615.  
  1616. extern JS_PUBLIC_API(JSBool)
  1617. JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc,
  1618.                 jsval *argv, jsval *rval);
  1619.  
  1620. extern JS_PUBLIC_API(JSBool)
  1621. JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, uintN argc,
  1622.                     jsval *argv, jsval *rval);
  1623.  
  1624. extern JS_PUBLIC_API(JSBool)
  1625. JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc,
  1626.                      jsval *argv, jsval *rval);
  1627.  
  1628. extern JS_PUBLIC_API(JSBranchCallback)
  1629. JS_SetBranchCallback(JSContext *cx, JSBranchCallback cb);
  1630.  
  1631. extern JS_PUBLIC_API(JSBool)
  1632. JS_IsRunning(JSContext *cx);
  1633.  
  1634. extern JS_PUBLIC_API(JSBool)
  1635. JS_IsConstructing(JSContext *cx);
  1636.  
  1637. /*
  1638.  * Returns true if a script is executing and its current bytecode is a set
  1639.  * (assignment) operation, even if there are native (no script) stack frames
  1640.  * between the script and the caller to JS_IsAssigning.
  1641.  */
  1642. extern JS_FRIEND_API(JSBool)
  1643. JS_IsAssigning(JSContext *cx);
  1644.  
  1645. /*
  1646.  * Set the second return value, which should be a string or int jsval that
  1647.  * identifies a property in the returned object, to form an ECMA reference
  1648.  * type value (obj, id).  Only native methods can return reference types,
  1649.  * and if the returned value is used on the left-hand side of an assignment
  1650.  * op, the identified property will be set.  If the return value is in an
  1651.  * r-value, the interpreter just gets obj[id]'s value.
  1652.  */
  1653. extern JS_PUBLIC_API(void)
  1654. JS_SetCallReturnValue2(JSContext *cx, jsval v);
  1655.  
  1656. /************************************************************************/
  1657.  
  1658. /*
  1659.  * Strings.
  1660.  *
  1661.  * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
  1662.  * on error (signified by null return), it leaves bytes owned by the caller.
  1663.  * So the caller must free bytes in the error case, if it has no use for them.
  1664.  * In contrast, all the JS_New*StringCopy* functions do not take ownership of
  1665.  * the character memory passed to them -- they copy it.
  1666.  */
  1667. extern JS_PUBLIC_API(JSString *)
  1668. JS_NewString(JSContext *cx, char *bytes, size_t length);
  1669.  
  1670. extern JS_PUBLIC_API(JSString *)
  1671. JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);
  1672.  
  1673. extern JS_PUBLIC_API(JSString *)
  1674. JS_NewStringCopyZ(JSContext *cx, const char *s);
  1675.  
  1676. extern JS_PUBLIC_API(JSString *)
  1677. JS_InternString(JSContext *cx, const char *s);
  1678.  
  1679. extern JS_PUBLIC_API(JSString *)
  1680. JS_NewUCString(JSContext *cx, jschar *chars, size_t length);
  1681.  
  1682. extern JS_PUBLIC_API(JSString *)
  1683. JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n);
  1684.  
  1685. extern JS_PUBLIC_API(JSString *)
  1686. JS_NewUCStringCopyZ(JSContext *cx, const jschar *s);
  1687.  
  1688. extern JS_PUBLIC_API(JSString *)
  1689. JS_InternUCStringN(JSContext *cx, const jschar *s, size_t length);
  1690.  
  1691. extern JS_PUBLIC_API(JSString *)
  1692. JS_InternUCString(JSContext *cx, const jschar *s);
  1693.  
  1694. extern JS_PUBLIC_API(char *)
  1695. JS_GetStringBytes(JSString *str);
  1696.  
  1697. extern JS_PUBLIC_API(jschar *)
  1698. JS_GetStringChars(JSString *str);
  1699.  
  1700. extern JS_PUBLIC_API(size_t)
  1701. JS_GetStringLength(JSString *str);
  1702.  
  1703. extern JS_PUBLIC_API(intN)
  1704. JS_CompareStrings(JSString *str1, JSString *str2);
  1705.  
  1706. /*
  1707.  * Mutable string support.  A string's characters are never mutable in this JS
  1708.  * implementation, but a growable string has a buffer that can be reallocated,
  1709.  * and a dependent string is a substring of another (growable, dependent, or
  1710.  * immutable) string.  The direct data members of the (opaque to API clients)
  1711.  * JSString struct may be changed in a single-threaded way for growable and
  1712.  * dependent strings.
  1713.  *
  1714.  * Therefore mutable strings cannot be used by more than one thread at a time.
  1715.  * You may call JS_MakeStringImmutable to convert the string from a mutable
  1716.  * (growable or dependent) string to an immutable (and therefore thread-safe)
  1717.  * string.  The engine takes care of converting growable and dependent strings
  1718.  * to immutable for you if you store strings in multi-threaded objects using
  1719.  * JS_SetProperty or kindred API entry points.
  1720.  *
  1721.  * If you store a JSString pointer in a native data structure that is (safely)
  1722.  * accessible to multiple threads, you must call JS_MakeStringImmutable before
  1723.  * retiring the store.
  1724.  */
  1725. extern JS_PUBLIC_API(JSString *)
  1726. JS_NewGrowableString(JSContext *cx, jschar *chars, size_t length);
  1727.  
  1728. /*
  1729.  * Create a dependent string, i.e., a string that owns no character storage,
  1730.  * but that refers to a slice of another string's chars.  Dependent strings
  1731.  * are mutable by definition, so the thread safety comments above apply.
  1732.  */
  1733. extern JS_PUBLIC_API(JSString *)
  1734. JS_NewDependentString(JSContext *cx, JSString *str, size_t start,
  1735.                       size_t length);
  1736.  
  1737. /*
  1738.  * Concatenate two strings, resulting in a new growable string.  If you create
  1739.  * the left string and pass it to JS_ConcatStrings on a single thread, try to
  1740.  * use JS_NewGrowableString to create the left string -- doing so helps Concat
  1741.  * avoid allocating a new buffer for the result and copying left's chars into
  1742.  * the new buffer.  See above for thread safety comments.
  1743.  */
  1744. extern JS_PUBLIC_API(JSString *)
  1745. JS_ConcatStrings(JSContext *cx, JSString *left, JSString *right);
  1746.  
  1747. /*
  1748.  * Convert a dependent string into an independent one.  This function does not
  1749.  * change the string's mutability, so the thread safety comments above apply.
  1750.  */
  1751. extern JS_PUBLIC_API(const jschar *)
  1752. JS_UndependString(JSContext *cx, JSString *str);
  1753.  
  1754. /*
  1755.  * Convert a mutable string (either growable or dependent) into an immutable,
  1756.  * thread-safe one.
  1757.  */
  1758. extern JS_PUBLIC_API(JSBool)
  1759. JS_MakeStringImmutable(JSContext *cx, JSString *str);
  1760.  
  1761. /************************************************************************/
  1762.  
  1763. /*
  1764.  * Locale specific string conversion callback.
  1765.  */
  1766. struct JSLocaleCallbacks {
  1767.     JSLocaleToUpperCase     localeToUpperCase;
  1768.     JSLocaleToLowerCase     localeToLowerCase;
  1769.     JSLocaleCompare         localeCompare;
  1770.     JSLocaleToUnicode       localeToUnicode;
  1771. };
  1772.  
  1773. /*
  1774.  * Establish locale callbacks. The pointer must persist as long as the
  1775.  * JSContext.  Passing NULL restores the default behaviour.
  1776.  */
  1777. extern JS_PUBLIC_API(void)
  1778. JS_SetLocaleCallbacks(JSContext *cx, JSLocaleCallbacks *callbacks);
  1779.  
  1780. /*
  1781.  * Return the address of the current locale callbacks struct, which may
  1782.  * be NULL.
  1783.  */
  1784. extern JS_PUBLIC_API(JSLocaleCallbacks *)
  1785. JS_GetLocaleCallbacks(JSContext *cx);
  1786.  
  1787. /************************************************************************/
  1788.  
  1789. /*
  1790.  * Error reporting.
  1791.  */
  1792.  
  1793. /*
  1794.  * Report an exception represented by the sprintf-like conversion of format
  1795.  * and its arguments.  This exception message string is passed to a pre-set
  1796.  * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
  1797.  * the JSErrorReporter typedef).
  1798.  */
  1799. extern JS_PUBLIC_API(void)
  1800. JS_ReportError(JSContext *cx, const char *format, ...);
  1801.  
  1802. /*
  1803.  * Use an errorNumber to retrieve the format string, args are char *
  1804.  */
  1805. extern JS_PUBLIC_API(void)
  1806. JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
  1807.                      void *userRef, const uintN errorNumber, ...);
  1808.  
  1809. /*
  1810.  * Use an errorNumber to retrieve the format string, args are jschar *
  1811.  */
  1812. extern JS_PUBLIC_API(void)
  1813. JS_ReportErrorNumberUC(JSContext *cx, JSErrorCallback errorCallback,
  1814.                      void *userRef, const uintN errorNumber, ...);
  1815.  
  1816. /*
  1817.  * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
  1818.  * Return true if there was no error trying to issue the warning, and if the
  1819.  * warning was not converted into an error due to the JSOPTION_WERROR option
  1820.  * being set, false otherwise.
  1821.  */
  1822. extern JS_PUBLIC_API(JSBool)
  1823. JS_ReportWarning(JSContext *cx, const char *format, ...);
  1824.  
  1825. extern JS_PUBLIC_API(JSBool)
  1826. JS_ReportErrorFlagsAndNumber(JSContext *cx, uintN flags,
  1827.                              JSErrorCallback errorCallback, void *userRef,
  1828.                              const uintN errorNumber, ...);
  1829.  
  1830. extern JS_PUBLIC_API(JSBool)
  1831. JS_ReportErrorFlagsAndNumberUC(JSContext *cx, uintN flags,
  1832.                                JSErrorCallback errorCallback, void *userRef,
  1833.                                const uintN errorNumber, ...);
  1834.  
  1835. /*
  1836.  * Complain when out of memory.
  1837.  */
  1838. extern JS_PUBLIC_API(void)
  1839. JS_ReportOutOfMemory(JSContext *cx);
  1840.  
  1841. struct JSErrorReport {
  1842.     const char      *filename;      /* source file name, URL, etc., or null */
  1843.     uintN           lineno;         /* source line number */
  1844.     const char      *linebuf;       /* offending source line without final \n */
  1845.     const char      *tokenptr;      /* pointer to error token in linebuf */
  1846.     const jschar    *uclinebuf;     /* unicode (original) line buffer */
  1847.     const jschar    *uctokenptr;    /* unicode (original) token pointer */
  1848.     uintN           flags;          /* error/warning, etc. */
  1849.     uintN           errorNumber;    /* the error number, e.g. see js.msg */
  1850.     const jschar    *ucmessage;     /* the (default) error message */
  1851.     const jschar    **messageArgs;  /* arguments for the error message */
  1852. };
  1853.  
  1854. /*
  1855.  * JSErrorReport flag values.  These may be freely composed.
  1856.  */
  1857. #define JSREPORT_ERROR      0x0     /* pseudo-flag for default case */
  1858. #define JSREPORT_WARNING    0x1     /* reported via JS_ReportWarning */
  1859. #define JSREPORT_EXCEPTION  0x2     /* exception was thrown */
  1860. #define JSREPORT_STRICT     0x4     /* error or warning due to strict option */
  1861.  
  1862. /*
  1863.  * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
  1864.  * has been thrown for this runtime error, and the host should ignore it.
  1865.  * Exception-aware hosts should also check for JS_IsExceptionPending if
  1866.  * JS_ExecuteScript returns failure, and signal or propagate the exception, as
  1867.  * appropriate.
  1868.  */
  1869. #define JSREPORT_IS_WARNING(flags)      (((flags) & JSREPORT_WARNING) != 0)
  1870. #define JSREPORT_IS_EXCEPTION(flags)    (((flags) & JSREPORT_EXCEPTION) != 0)
  1871. #define JSREPORT_IS_STRICT(flags)       (((flags) & JSREPORT_STRICT) != 0)
  1872.  
  1873. extern JS_PUBLIC_API(JSErrorReporter)
  1874. JS_SetErrorReporter(JSContext *cx, JSErrorReporter er);
  1875.  
  1876. /************************************************************************/
  1877.  
  1878. /*
  1879.  * Regular Expressions.
  1880.  */
  1881. #define JSREG_FOLD      0x01    /* fold uppercase to lowercase */
  1882. #define JSREG_GLOB      0x02    /* global exec, creates array of matches */
  1883. #define JSREG_MULTILINE 0x04    /* treat ^ and $ as begin and end of line */
  1884.  
  1885. extern JS_PUBLIC_API(JSObject *)
  1886. JS_NewRegExpObject(JSContext *cx, char *bytes, size_t length, uintN flags);
  1887.  
  1888. extern JS_PUBLIC_API(JSObject *)
  1889. JS_NewUCRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags);
  1890.  
  1891. extern JS_PUBLIC_API(void)
  1892. JS_SetRegExpInput(JSContext *cx, JSString *input, JSBool multiline);
  1893.  
  1894. extern JS_PUBLIC_API(void)
  1895. JS_ClearRegExpStatics(JSContext *cx);
  1896.  
  1897. extern JS_PUBLIC_API(void)
  1898. JS_ClearRegExpRoots(JSContext *cx);
  1899.  
  1900. /* TODO: compile, exec, get/set other statics... */
  1901.  
  1902. /************************************************************************/
  1903.  
  1904. extern JS_PUBLIC_API(JSBool)
  1905. JS_IsExceptionPending(JSContext *cx);
  1906.  
  1907. extern JS_PUBLIC_API(JSBool)
  1908. JS_GetPendingException(JSContext *cx, jsval *vp);
  1909.  
  1910. extern JS_PUBLIC_API(void)
  1911. JS_SetPendingException(JSContext *cx, jsval v);
  1912.  
  1913. extern JS_PUBLIC_API(void)
  1914. JS_ClearPendingException(JSContext *cx);
  1915.  
  1916. extern JS_PUBLIC_API(JSBool)
  1917. JS_ReportPendingException(JSContext *cx);
  1918.  
  1919. /*
  1920.  * Save the current exception state.  This takes a snapshot of cx's current
  1921.  * exception state without making any change to that state.
  1922.  *
  1923.  * The returned state pointer MUST be passed later to JS_RestoreExceptionState
  1924.  * (to restore that saved state, overriding any more recent state) or else to
  1925.  * JS_DropExceptionState (to free the state struct in case it is not correct
  1926.  * or desirable to restore it).  Both Restore and Drop free the state struct,
  1927.  * so callers must stop using the pointer returned from Save after calling the
  1928.  * Release or Drop API.
  1929.  */
  1930. extern JS_PUBLIC_API(JSExceptionState *)
  1931. JS_SaveExceptionState(JSContext *cx);
  1932.  
  1933. extern JS_PUBLIC_API(void)
  1934. JS_RestoreExceptionState(JSContext *cx, JSExceptionState *state);
  1935.  
  1936. extern JS_PUBLIC_API(void)
  1937. JS_DropExceptionState(JSContext *cx, JSExceptionState *state);
  1938.  
  1939. /*
  1940.  * If the given value is an exception object that originated from an error,
  1941.  * the exception will contain an error report struct, and this API will return
  1942.  * the address of that struct.  Otherwise, it returns NULL.  The lifetime of
  1943.  * the error report struct that might be returned is the same as the lifetime
  1944.  * of the exception object.
  1945.  */
  1946. extern JS_PUBLIC_API(JSErrorReport *)
  1947. JS_ErrorFromException(JSContext *cx, jsval v);
  1948.  
  1949. /*
  1950.  * Given a reported error's message and JSErrorReport struct pointer, throw
  1951.  * the corresponding exception on cx.
  1952.  */
  1953. extern JS_PUBLIC_API(JSBool)
  1954. JS_ThrowReportedError(JSContext *cx, const char *message,
  1955.                       JSErrorReport *reportp);
  1956.  
  1957. #ifdef JS_THREADSAFE
  1958.  
  1959. /*
  1960.  * Associate the current thread with the given context.  This is done
  1961.  * implicitly by JS_NewContext.
  1962.  *
  1963.  * Returns the old thread id for this context, which should be treated as
  1964.  * an opaque value.  This value is provided for comparison to 0, which
  1965.  * indicates that ClearContextThread has been called on this context
  1966.  * since the last SetContextThread, or non-0, which indicates the opposite.
  1967.  */
  1968. extern JS_PUBLIC_API(jsword)
  1969. JS_GetContextThread(JSContext *cx);
  1970.  
  1971. extern JS_PUBLIC_API(jsword)
  1972. JS_SetContextThread(JSContext *cx);
  1973.  
  1974. extern JS_PUBLIC_API(jsword)
  1975. JS_ClearContextThread(JSContext *cx);
  1976.  
  1977. #endif /* JS_THREADSAFE */
  1978.  
  1979. /************************************************************************/
  1980.  
  1981. JS_END_EXTERN_C
  1982.  
  1983. #endif /* jsapi_h___ */
  1984.